<template>
	<view class="container">
		<pyh-nv :config="config"></pyh-nv>
		<image :src="baseurl+'user/setting.png'" class="bg"></image>
		<button class="avatar-wrapper" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
			<image :src="avatar" class="avatar"></image>
		</button>
		<view class="tips">
			<image :src="baseurl+'user/modify.png'" class="icon"></image>
			<text class="text">点击修改</text>
		</view>
		<view class="nameBox">
			<input type="text" class="name" v-model="name" placeholder="用户名" />
		</view>
		<view class="submit" @click="submit">
			保存
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				baseurl: this.$baseurl,
				config: {
					// back: {
					// 	hide: true
					// },
					bgColor: "transparent",
					title: " ",
					color: '#ffffff'
				},
				name: '',
				avatar: 'https://miantou.guguan.net/img/user/avatar.png',
				uploadAvatar: ''
			}
		},
		onLoad(options) {
			// this.id = options.id;
		},
		methods: {
			onChooseAvatar(e) {
				uni.uploadFile({
					url: `https://miantou.guguan.net/api/File/formImage`,
					header: {
						"Content-Type": "multipart/form-data",
						"token": JSON.parse(uni.getStorageSync('TOKEN')).value
					},
					filePath: e.target.avatarUrl,
					name: "file",
					fileType: "image",
					success: (res) => { //返回成功更新头像
						let data = JSON.parse(res.data);
						console.log(data.data);
						this.avatar = data.data.uri;
						this.uploadAvatar=this.avatar;
						uni.setStorageSync('avatar', this.avatar);
					},
					fail: (res) => {
						this.$u.toast('上传失败，请重新上传')
					}
				});
			},
			submit() {
				if (this.uploadAvatar == '') {
					this.$u.toast('请上传头像')
					return;
				}
				if (this.name == '') {
					this.$u.toast('请输入用户名')
					return;
				}
				this.$request.post(`user/update_user_data`, {
					nickname: this.name,
					avatar: this.uploadAvatar,
				}).then(res => {
					if (res.data.code > 0) { //预约成功
						this.$u.toast('提交成功')
					} else {
						this.$u.toast(res.data.msg)
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.nameBox {
		position: relative;
		width: 674rpx;
		height: 88rpx;
		margin-top: 160rpx;
		margin-left: 39rpx;
		border-radius: 10rpx;
		display: flex;
		align-items: center;
		background-color: #fff;

		.name {
			width: 100%;
			height: 100%;
			margin-left: 10rpx;
			border: none;
			text-align: left;
			font-size: 26rpx;
			color: #000000;
		}
	}

	.submit {
		position: relative;
		width: 500rpx;
		height: 90rpx;
		border-radius: 45rpx;
		margin-left: 125rpx;
		margin-top: 100rpx;
		font-size: 28rpx;
		text-align: center;
		color: #FAFAFA;
		line-height: 90rpx;
		background-color: #4E97AD;
	}

	.nameInput {
		width: 650rpx;
		height: 70rpx;
		border: 1px solid #ffffff;
		margin-top: 94rpx;
		left: 40rpx;
		text-align: left;
		color: #fff;
		padding-left: 20rpx;
		font-size: 24rpx;
	}

	.tips {
		position: relative;
		width: 100%;
		height: 22rpx;
		line-height: 22rpx;
		margin-top: 60rpx;

		.text {
			height: 22rpx;
			font-size: 22rpx;
			line-height: 22rpx;
			color: #919191;
			margin-left: 25rpx;
		}

		.icon {
			width: 22rpx;
			height: 22rpx;
			margin-left: 290rpx;
		}
	}

	.avatar-wrapper {
		width: 128rpx;
		height: 128rpx;
		margin-top: 115rpx;
		margin-left: 311rpx;
		border-radius: 50%;

		.avatar {
			width: 128rpx;
			height: 128rpx;
			top: 0;
			border-radius: 50%;
		}
	}

	.container {
		position: fixed;
		width: 100%;
		height: 100vh;
		overflow-y: hidden;
		background-color: #F5F7FB;
		overflow-x: hidden;
	}

	.bg {
		position: absolute;
		width: 750rpx;
		height: 1623rpx;
		left: 0;
		top: 0;
	}
</style>